/* *python database pi v2.0 compliant*/
var DEBUG = true;//makes use of dump method
var db = {
    apilevel:'2.0',
    threadsafety:0,
    paramstyle:'format',
    Date:function(year,month,day) {},
    Time:function(hour,minute,second) {},
    Timestamp:function(year,month,day,hour,second) {},
    DateFromTicks:function(ticks) {},
    TimeFromTicks:function(ticks) {},
    TimestampFromTicks:function(ticks) {},
    Binary:function(string) {},
    STRING:String,
    NUMBER:Number,
    ROWID:Number
};
//base error class
db.Error = function(string) {
    //it's implied that when the constructor is being called,
    //execute 
    
};
db.Error.__proto__=Error.prototype;

db.InterfaceError=function() {};
db.InterfaceError.__proto__=db.Error.prototype;
db.DataBaseError= function() {};
db.DataBaseError.__proto__=db.Error.prototype;
db.Connection = function(conn_params,remoteUrl,_callback) {
    this._conn_info = conn_params;
    this.remoteUrl = remoteUrl;
    var request = db.getTransport();
    request.onreadystatechange=function() {
	if(request.readyState==4 && request.status==200) {db.Connection.callback(request,_callback)}
    }
    request.open("POST",remoteUrl,true);
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    request.send(this.toString());
};
db.Connection.callback = function(res,_callback) {
    dump(res.responseText);
    if(!res.responseXML) {
	_callback.failure(res);
    }
    _callback.success(res);
};

db.connect = function () {
    try {
	var _conn =  new db.Connection(arguments[0],arguments[1],arguments[2]);
    } catch(err) {
	dump(err);
    }
    return _conn;
};
db.Connection.prototype.close = function() {
    for(i in this) {
	this[i]=null;
    }
};//probably useless given that http connections aren't maintained
db.Connection.prototype.commit = function() { return null};//transactions don't work across requests
db.Connection.prototype.cursor = function() { return new db.Cursor(this)};//key method
db.Connection.prototype.toString = function() { return db.serialize(this._conn_info)}
db.Cursor = function(conn) {
    //don't call this method directly, use conn.cursor() to instantiate with a given connection
    this.Connection=conn;	
};
db.Cursor.prototype.rowcount = -1;
db.Cursor.prototype.callproc = function(procname) {};
db.Cursor.prototype.close = function() {};
db.Cursor.prototype.execute = function(operation,parameters,_callback) {
    if(parameters) {/* subst vars*/}
    var request=db.getTransport();//not ideal, no possibility of reusing objects
    request.onreadystatechange=function() {
	if(request.readyState==4 && request.status==200) { db.Connection.callback(_callback,request);}
    }
    request.open("POST",this.Connection.remoteUrl,true);
    request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

    request.send(this.Connection.toString()+'&operation='+encodeURIComponent(operation));
};
db.Cursor.prototype.executemany = function(operation,seq_of_parameters) {
    for(var i=0;i<seq_of_parameters.length;i++) {
	this.execute(operation,seq_of_parameters[i]);
    }
    return;
};
db.Cursor.prototype.fetchone = function() {};
db.Cursor.prototype.fetchmany = function(size) {
    size = size || this.arraysize;
};
db.Cursor.prototype.fetchall = function() {return};
db.Cursor.prototype.arraysize=1;
db.Cursor.prototype.setintputsize = function(size) { return};
db.Cursor.prototype.setoutputsize = function(size,column) { return};
db.getTransport= function () {
    if (typeof XMLHttpRequest != "undefined") {
	return new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
	return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
	throw new Error("XMLHttpRequest not supported");
    }
}
db.serialize = function(obj) {
    response=new Array();
    for(i in obj) {
	response.push([encodeURIComponent(i),encodeURIComponent(obj[i])].join('='));
    }
    return response.join('&');
};
